<?php

function load_lists()
{
	global $distros, $archs, $repo_name, $mirrors;
	foreach ($distros as $distro) {
		foreach (array_keys($repo_name) as $repo) {
			foreach ($archs as $arch) {
				$file = "lists/ru/$repo.$distro.$arch.list";
				$filedata = @file($file);
				if (!$filedata)
					continue;
				foreach ($filedata as $mirrorstring) {
					/* alt partial implementation
					$newmirror = array();
					foreach ($data in explode(",", $mirrorstring)) {
						list($var, $value) = explode("=", $data, 2);
						if ($var == "country" || $var == "url")
							$newmirror[$var] = $value;
					}
					if ($newmirror["type"] != "distrib")
						continue;
					unset($newmirror["type"]);
					$mirrors[$repo][$distro][$arch][] = $newmirror;
					*/
					preg_match("/,country=([^,]+).*,type=([^,]+).*,url=(([^:]+:\/\/([^\/,]+))[^,]*)/", $mirrorstring, $matches);
					if ($matches[2] != "distrib")
						continue;
					$mirrors[$repo][$distro][$arch][] = array("country" => $matches[1], "url" => trim($matches[3]), "short" => $matches[4], "host" => $matches[5]);
				}
			}
		}
}	
}

function init_version_arch()
{
	global $version, $arch, $distros, $archs, $manual_mirror, $default_distro, $default_arch;
	$version = $_GET['version'];
	$arch = $_GET['arch'];

#	if (!in_array($version, $distros))
#		$version = $manual_mirror ? $default_distro : "2011.0";

#	if (!in_array($arch, $archs))
#		$arch = $manual_mirror ? $default_arch : "i586";

}

function get_mirror_url($repo)
{
	global $version, $arch, $mirrors;

	if (!$version || !$arch)
		init_version_arch();

	if (!$_GET['mirror'][$repo])
		return false;

	foreach ($mirrors[$repo][$version][$arch] as $mirror) {
		if ($mirror['short'] == $_GET['mirror'][$repo])
			return $mirror['url'];
	}
 	return false;
}

function get_list_url($repo)
{
	global $list_template, $auto_list, $version, $arch;

	if (!$version || !$arch)
		init_version_arch();

	if ($version != "auto" || $arch != "auto") {
		$arch = $arch == "auto" ? '$ARCH' : $arch;
		$distro = $version == "auto" ? '$RELEASE' : $version;
		return $list_template[$repo] . "$distro.$arch.list";
	}
	return $auto_list[$repo];
}

function sort_mirrors($a, $b) {
	if ($a['country'] > $b['country'])
		return 1;
	if ($a['country'] < $b['country'])
		return -1;
	if ($a['host'] > $b['host'])
		return 1;
	if ($a['host'] < $b['host'])
		return -1;
	if ($a['short'] > $b['short'])
		return 1;
	if ($a['short'] < $b['short'])
		return -1;
	return 0;
}

function simplify_url($url) {
	$url = explode('/', $url);
	$spliced_count = 0;
	foreach (array_keys($url, '..') as $i => $part) {
		$index = $part - $spliced_count - 1;
		if ($index < 0 || $url[$index] == "..")
			continue;
		array_splice($url, $index, 2);
		$spliced_count += 2;
	}
	return implode('/', $url);
}

// This function cmp a and b, permit usort
// the goal is to have 10.0 after 9.2
function sort_version ($a, $b) {
	$la = split("\.",$a);
	$lb = split("\.",$b);
	for ($i=0; $i <= (count($la) < count($lb) ? count($la) : count($lb)); $i++) {
		if ($la[$i] > $lb[$i]) return -1;
		if ($la[$i] < $lb[$i]) return 1;
	}
	return 0;
}


?>
